/* putchar example: printing alphabet */
#include <stdio.h>
#include <ctype.h>


//==============================================================================
//Step 2A
//==============================================================================
/*
int main ()
{
  int index = 0, lowChar = 0, upChar = 0;
  char ch;
  printf("Enter a line of text:\n");
  // Read every char from input
  while(ch != EOF && (ch != '\n')) 
  {
    ch = getchar();
    // change upper to lower
    if (isupper(ch))
    {
      ch = tolower(ch);
      lowChar ++;
      
    }
    // else change lower to upper
    else if (islower(ch))
    {
      ch = toupper(ch);
      upChar ++;
      
    }
    // record the number of chars
    index++;
    putchar(ch);
    
  }
  //print out the info
  printf("Read %d characters in total", index);
  printf(", %d converted to upper-case", upChar);
  printf(", %d to lower-case\n", lowChar);
  
  return 0;
  
}
*/
//==============================================================================
//Step 2B
//==============================================================================
/*
int main ()
{
  FILE *fptr;
  int index = 0, lowChar = 0, upChar = 0;
  char ch;
  
  // open inout file in read-only mode
  fptr = fopen("input", "r");
  
   
  while(!feof(fptr) && (ch != '\n')) 
  {
    ch = getc(fptr);
    if (isupper(ch))
    {
      ch = tolower(ch);
      lowChar ++;
      
    }
    else if (islower(ch))
    {
      ch = toupper(ch);
      upChar ++;
      
    }
    index++;
    putchar(ch);
    
  }
  printf("Read %d characters in total", index);
  printf(", %d converted to upper-case", upChar);
  printf(", %d to lower-case\n", lowChar);
  
  fclose(fptr);
  return 0;
  
}
*/
//==============================================================================
//Step 2C
//==============================================================================
/*
int main ()
{
  FILE *fptr;
  int index = 0, lowChar = 0, upChar = 0;
  char ch;
  
  // open text_file in read-only mode
  fptr = fopen("input", "r");
  // input file must exist
  if (fptr1 == NULL) 
    perror ("Error with input file");
  else
  {
    while(!feof(fptr) && (ch != '\n')) 
    {
      ch = getc(fptr);
      if (isupper(ch))
      {
        ch = tolower(ch);
        lowChar ++;
      
      }
      else if (islower(ch))
      {
        ch = toupper(ch);
        upChar ++;
      
      }
      index++;
      putchar(ch);
    
    }
    printf("Read %d characters in total", index);
    printf(", %d converted to upper-case", upChar);
    printf(", %d to lower-case\n", lowChar);
    // close the input file
    fclose(fptr);
    return 0;
  } 
}
*/
//==============================================================================
//Step 2C
//==============================================================================
/*
int main ()
{
  FILE *fptr1, *fptr2;
  int index = 0, lowChar = 0, upChar = 0;
  char ch;
  
  // open files for reading and writing
  fptr1 = fopen("input", "r");
  fptr2 = fopen("output", "w");
  // input file must exsit
  if (fptr1 == NULL) 
    perror ("Error with input file");
  // output file must writeable
  if (!fptr2)
    printf("Error with output file, maybe it cannot be written in\n");
  else
  { 
    while(!feof(fptr1) && (!feof(fptr2))&& (ch != '\n')) 
    {
      ch = getc(fptr1);
      if (isupper(ch))
      {
        ch = tolower(ch);
        lowChar ++;
      
      }
      else if (islower(ch))
      {
        ch = toupper(ch);
        upChar ++;
      }
      index++;
      putc(ch, fptr2);
    
     }
    // print the info into the output file
    fprintf(fptr2, "Read %d characters in total", index);
    fprintf(fptr2, ", %d converted to upper-case", upChar);
    fprintf(fptr2, ", %d to lower-case\n", lowChar);
    // close the file
    fclose(fptr1);
    fclose(fptr2);
    return 0;
  }
}
*/
//==============================================================================
//Step 2D
//=============================================================================

int main ()
{
  FILE *fptr1, *fptr2;
  int index = 0, lowChar = 0, upChar = 0;
  char ch;
  char input_file[80];
  char output_file[80];
  // scan the inout and output file
  printf("Enter the input file name: ");
  scanf("%s", input_file);
  printf("Enter the output file name: ");
  scanf("%s", output_file);
  
  // open files for reading and writing
  
  fptr1 = fopen(input_file, "r");
  fptr2 = fopen(output_file, "w");
  if (fptr1 == NULL) 
    perror ("Error with input file");
  if (!fptr2)
    printf("Error with output file %s , maybe it cannot be written in\n", output_file);
  else
  {
    while(!feof(fptr1) && (!feof(fptr2))&& (ch != '\n')) 
    {
      ch = getc(fptr1);
      if (isupper(ch))
      {
        ch = tolower(ch);
        lowChar ++;
      
      }
      else if (islower(ch))
      {
        ch = toupper(ch);
        upChar ++;
      
      }
      index++;
      putc(ch, fptr2);
    
    }
    fprintf(fptr2, "Read %d characters in total", index);
    fprintf(fptr2, ", %d converted to upper-case", upChar);
    fprintf(fptr2, ", %d to lower-case\n", lowChar);
  
    fclose(fptr1);
    fclose(fptr2);
    return 0;
  }
}












